package com.bdmc.web.controller.base;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.base.domain.vo.BaseFacilityVo;
import com.bdmc.base.domain.bo.BaseFacilityBo;
import com.bdmc.base.service.IBaseFacilityService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 设施管理控制器
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/facility")
public class BaseFacilityController extends BaseController {

    private final IBaseFacilityService iBaseFacilityService;

    /**
     * 查询设施管理列表
     */
    @SaCheckPermission("base:facility:list")
    @GetMapping("/list")
    public TableDataInfo<BaseFacilityVo> list(@Validated(QueryGroup.class) BaseFacilityBo bo, PageQuery pageQuery) {
        return iBaseFacilityService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出设施管理列表
     */
    @SaCheckPermission("base:facility:export")
    @Log(title = "设施管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BaseFacilityBo bo, HttpServletResponse response) {
        List<BaseFacilityVo> list = iBaseFacilityService.queryList(bo);
        ExcelUtil.exportExcel(list, "设施管理", BaseFacilityVo.class, response);
    }

    /**
     * 获取设施管理详细信息
     */
    @SaCheckPermission("base:facility:query")
    @GetMapping("/{facilityId}")
    public AjaxResult<BaseFacilityVo> getInfo(@PathVariable("facilityId") Long facilityId) {
        return AjaxResult.success(iBaseFacilityService.queryById(facilityId));
    }

    /**
     * 新增设施管理
     */
    @SaCheckPermission("base:facility:add")
    @Log(title = "设施管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BaseFacilityBo bo) {
        return toAjax(iBaseFacilityService.insertByBo(bo));
    }

    /**
     * 修改设施管理
     */
    @SaCheckPermission("base:facility:edit")
    @Log(title = "设施管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BaseFacilityBo bo) {
        return toAjax(iBaseFacilityService.updateByBo(bo));
    }

    /**
     * 绑定设施与设备
     */
    @SaCheckPermission("base:facility:edit")
    @Log(title = "绑定设施与设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("bind")
    public AjaxResult<Void> bind(@Validated(EditGroup.class) @RequestBody BaseFacilityBo bo) {
        return toAjax(iBaseFacilityService.bindDevice(bo));
    }

    /**
     * 解除绑定设施与设备
     */
    @SaCheckPermission("base:facility:edit")
    @Log(title = "解除绑定设施与设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("unbind")
    public AjaxResult<Void> unbind(@Validated(EditGroup.class) @RequestBody BaseFacilityBo bo) {
        return toAjax(iBaseFacilityService.unbindDevice(bo));
    }

    /**
     * 删除设施管理
     */
    @SaCheckPermission("base:facility:remove")
    @Log(title = "设施管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{facilityIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] facilityIds) {
        return toAjax(iBaseFacilityService.deleteWithValidByIds(Arrays.asList(facilityIds), true));
    }
}
